<template>
  <a-drawer
    title="分数线设置"
    width=450
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="editVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="院系">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['zsyxm',{initialValue:postParams.zsyxm}]" @change="yxChange">
              <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="专业">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['zszym',{initialValue:postParams.zszym}]">
              <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{i.zyh+'-'+i.zymc }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="招生年度">
            <a-select :allowClear="true" showSearch optionFilterProp="children"  v-decorator="['zsnj',{initialValue:postParams.zsnj}]">
              <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="学习形式">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['isqrz',{initialValue:postParams.isqrz}]" @change="xxChange">
              <a-select-option v-for="i in selectMenu.xxlx" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="研究方向">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['yjfxm',
                  {
                      rules: [{ required: true, message: '请输入研究方向!' }],
                  },{initialValue:postParams.yjfxm}]" >
              <a-select-option v-for="i in selectMenu.fx" :key="i.yjfxm" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>


        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="科目一">
            <a-input placeholder="请输入..."  v-decorator="['kmyfsx',{initialValue:postParams.kmyfsx}]" @change="fsChange"/>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="科目二">
            <a-input placeholder="请输入..."  v-decorator="['kmefsx',{initialValue:postParams.kmefsx}]" @change="fsChange"/>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="科目三">
            <a-input placeholder="请输入..."  v-decorator="['kmsfsx',{initialValue:postParams.kmsfsx}]" @change="fsChange"/>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="科目四">
            <a-input placeholder="请输入..."  v-decorator="['kmffsx',{initialValue:postParams.kmffsx}]" @change="fsChange"/>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}"  >
          <a-form-item label="总分">
            <a-input placeholder="请输入..."  v-model="zffsx" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  // import moment from 'moment'
  import { STable } from '~'
  import { mapState } from 'vuex'

  const formItemLayout = {
    labelCol: { span: 3 },
    wrapperCol: { span: 18 }
  }

  const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
  export default {
    props: {
      editVisible: {
        default: false
      },
      info: {
        default: () => ({})
      }
    },
    components: {
      STable
    },
    data () {
      return {
        formItemLayout,
        loading: false,
        postParams: {
        },
        selectMenu: {
          yx: [] ,// 院系
          zy:[],
          xxlx:[]
        },
        zffsx:''
      }
    },
    beforeCreate () {
      this.form = this.$form.createForm(this)
    },
    watch:{
      info(){
        console.log(this.info)
        this.form.setFieldsValue({'zsyxm':this.info.zsyxm})
        this.form.setFieldsValue({'zszym':this.info.zszym})
        this.form.setFieldsValue({'zsnj':this.info.zsnj})
        this.form.setFieldsValue({'isqrz':this.info.isqrz})
        // this.form.setFieldsValue({'yjfxm':this.info.yjfxm})
        this.form.getFieldDecorator('yjfxm', {initialValue: this.info.yjfxm})
        this.form.setFieldsValue({'kmyfsx':this.info.kmyfsx})
        this.form.setFieldsValue({'kmefsx':this.info.kmefsx})
        this.form.setFieldsValue({'kmsfsx':this.info.kmsfsx})
        this.form.setFieldsValue({'kmffsx':this.info.kmffsx})
        this.postParams = this.info;
        this.zffsx=this.info.zffsx
        this.getProssion(this.info.zsyxm)
        let params={
          ssyxm:this.postParams.zsyxm,
          sszym:this.postParams.zszym,
          xwccm:2,
          zsxn:this.postParams.zsnj
        }
        this.$api.base.researchAll(params).then(res=>{
          this.$set(this.selectMenu,'fx',res.data)
        })
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'] // 年级
      })
    },
    created () {
      this.initSelectMenu()
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
    },
    methods: {
      initSelectMenu () {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
          this.$api.base.dictionaryData('TYPE0441').then(res => {
            this.selectMenu.xxlx = res.data
          })

        })
      },
      reset () {
        this.data = []
      },
      // 选择院系
      yxChange (code) {
        // this.$set(this.postParams, 'zszym', '')
        this.form.setFieldsValue({'zszym':''})
        this.getProssion(code)
      },
      fsChange(){

        var total = 0
        if(parseInt(this.form.getFieldsValue().kmyfsx)){
          total +=parseInt(this.form.getFieldsValue().kmyfsx)
        }
        if(parseInt(this.form.getFieldsValue().kmefsx)){
          total +=parseInt(this.form.getFieldsValue().kmefsx)
        }
        if(parseInt(this.form.getFieldsValue().kmsfsx)){
          total +=parseInt(this.form.getFieldsValue().kmsfsx)
        }
        if(parseInt(this.form.getFieldsValue().kmffsx)){
          total +=parseInt(this.form.getFieldsValue().kmffsx)
        }
        // var total = parseInt(this.postParams.kmyfsx) + parseInt(this.postParams.kmefsx) +parseInt(this.postParams.kmsfsx)+parseInt(this.postParams.kmffsx)
        this.zffsx=total

      },
      getProssion(code){
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      },
      xxChange (code) {
        this.form.setFieldsValue({'yjfxm':''})
        this.$set(this.selectMenu,'fx','')
        if(code){
          let params={
            ssyxm:this.form.getFieldsValue().zsyxm,
            sszym:this.form.getFieldsValue().zszym,
            xwccm:2,
            zsxn:this.form.getFieldsValue().zsnj,
            qrzhfqrz:code
          }
          this.$api.base.researchAll(params).then(res=>{
            this.$set(this.selectMenu,'fx',res.data)
          })
        }
      },
      // 关闭
      onClose () {
        this.reset()
        this.$emit('close')
      },
      // 保存
      handleSubmit () {
        this.postParams.zffsx = this.zffsx
        var params = Object.assign({'zffsx':this.postParams.zffsx,'id':this.info.id},this.form.getFieldsValue())
        this.$api.recruitstat.reExamCoreLineMangegeUpdate(params).then(res => {
          this.reset()
          this.$emit('success')
        }).finally(() => {
          this.loadingBtn = false
        })
      },
      handleDelete (key) {
        const dataSource = [...this.data]
        this.data = dataSource.filter(item => item.key !== key)
      },
      kmyChange(data){
        this.postParams.kmyfsx = data.data
        console.log('科目一改变',value)
      },
      kmeChange(value){
        this.postParams.kmefsx = value
      },
      kmsChange(value){
        this.postParams.kmsfsx = value
      },
      kmrChange(value){
        this.postParams.kmffsx = value
      },
    }
  }
</script>
